﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SWDataDB.DAL;
using SWDataDB.Models;
using System.Data.SqlClient;

namespace SWDataDB.DAL
{
    /// <summary>
    /// 2013-5-19修改完成
    /// </summary>
    public class YPInfo
    {
        public YPInfo() { }

        /// <summary>
        /// 获取所有信息
        /// </summary>
        /// <returns>返回dataSet</returns>
        public DataSet GetAllInfoBySet()
        {
            string strSql = "SELECT * FROM dbo.YP_INFO_TBL";
            return SQLHelper.ExecuteDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        /// <summary>
        /// 查询出所有样品信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllInfoByTable()
        {
            string strSql = "SELECT *FROM dbo.YP_INFO_TBL";

            return SQLHelper.ExecuteReaderDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        /// <summary>
        /// 得到记录for 分页显示，升序排列
        /// </summary>
        /// <param name="page"></param>
        /// <param name="num"></param>
        /// <param name="strExp"></param>
        /// <returns>返回dataset</returns>
        public DataSet GetInfoForPageByASC(string strExp)
        {
            string strSql = "SELECT * FROM dbo.YP_INFO_TBL WHERE ";
            strSql += strExp;
            strSql += "ORDER BY ID ASC";
            return SQLHelper.ExecuteDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        /// <summary>
        /// 得到著录表记录for 分页显示，降序排列
        /// </summary>
        /// <param name="page"></param>
        /// <param name="num"></param>
        /// <param name="strExp"></param>
        /// <returns>返回dataset</returns>
        public DataSet GetInfoForPageByDESC(string strExp)
        {
            string strSql = "SELECT * FROM dbo.YP_INFO_TBL WHERE ";
            strSql += strExp;
            strSql += "ORDER BY ID DESC";
            return SQLHelper.ExecuteDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        /// <summary>
        /// 根据ID查找
        /// </summary>
        /// <param name="page"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        /// 
        public DataSet SelectByYPID(int ypid)
        {
            string strSql = "SELECT * FROM dbo.YP_INFO_TBL WHERE ID =";
            strSql += ypid;

            return SQLHelper.ExecuteDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }



        /// <summary>
        /// 分页查找
        /// </summary>
        /// <param name="page"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        /// 




        public IList<YP_INFO_TBL> GetInfoByPage(long page, long num, string strWhere)
        {
            IList<YP_INFO_TBL> ypLst = new List<YP_INFO_TBL>(); 
            string strSql = string.Empty;
            strSql = SQLHelper.GetPagingSQL("YP_INFO_TBL", "ID", "*", strWhere, "ID DESC", page, num);

            try
            {
                DataTable dt = SQLHelper.ExecuteReaderDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
                foreach (DataRow dr in dt.Rows)
                {
                    YP_INFO_TBL yp_info = new YP_INFO_TBL();
                    yp_info.O_ID = int.Parse(dr["ID"].ToString());
                    yp_info.O_ZL_FILE_ID = dr["ZL_FILE_ID"].ToString();
                    yp_info.O_YP_CODE = dr["YP_CODE"].ToString();
                    yp_info.O_YP_NAME = dr["YP_NAME"].ToString();
                    yp_info.O_PM_PATH_NAME = dr["PM_PATH_NAME"].ToString();
                    yp_info.O_PM_PATH_NUM = dr["PM_PATH_NUM"].ToString();
                    yp_info.O_GEO_ATT_NUM = dr["GEO_ATT_NUM"].ToString();
                    if (dr["YP_LAT"].ToString() != "")
                        yp_info.O_YP_LAT = Convert.ToInt32(dr["YP_LAT"].ToString());
                    if (dr["YP_LON"].ToString() != "")
                        yp_info.O_YP_LON = Convert.ToInt32(dr["YP_LON"].ToString());
                    yp_info.O_YP_STYLE = dr["YP_STYLE"].ToString();
                    yp_info.O_STYLE_CODE = dr["STYLE_CODE"].ToString();
                    yp_info.O_BP_ID = dr["BP_ID"].ToString();
                    yp_info.O_OUT_NAME = dr["OUT_NAME"].ToString();
                    yp_info.O_INNAR_NAME = dr["INNAR_NAME"].ToString();
                    // Image数据暂时不做处理
                    // bpInfo.O_IMAGE = dr["IMAGE"].ToString();
                    yp_info.O_REMARK = dr["REMARK"].ToString();
                    yp_info.O_CY_POS = dr["CY_POS"].ToString();
                    yp_info.O_CY_LEVEL = dr["CY_LEVEL"].ToString();
                    yp_info.O_CJ_PEOPLE = dr["CJ_PEOPLE"].ToString();
                    yp_info.O_CJ_DATE = dr["CJ_DATE"].ToString();

                    ypLst.Add(yp_info);
                }

                return ypLst;
            }
            catch (FormatException e)
            {
                throw e;
            }
            catch (OverflowException e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 通过fileID查询记录
        /// </summary>
        /// <param name="fileId"></param>
        /// <returns></returns>
        public YP_INFO_TBL SelectYPInfoByID(string yp_code)
        {
            YP_INFO_TBL yp_info = new YP_INFO_TBL();
            string strSql = "SELECT * FROM dbo.YP_INFO_TBL WHERE YP_CODE ='";
            strSql += yp_code + "'";
            try
            {
                DataTable dt = SQLHelper.ExecuteReaderDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
                foreach (DataRow dr in dt.Rows)
                {
                    yp_info.O_ID = int.Parse(dr["ID"].ToString());
                    yp_info.O_YP_CODE = yp_code;
                    yp_info.O_YP_NAME = dr["YP_NAME"].ToString();
                    yp_info.O_PM_PATH_NAME = dr["PM_PATH_NAME"].ToString();
                    yp_info.O_PM_PATH_NUM = dr["PM_PATH_NUM"].ToString();
                    yp_info.O_GEO_ATT_NUM = dr["GEO_ATT_NUM"].ToString();
                    if (dr["YP_LAT"].ToString() != "")
                        yp_info.O_YP_LAT= Convert.ToInt32(dr["YP_LAT"].ToString());
                    if (dr["YP_LON"].ToString() != "")
                        yp_info.O_YP_LON = Convert.ToInt32(dr["YP_LON"].ToString());
                    yp_info.O_YP_STYLE = dr["YP_STYLE"].ToString();
                    yp_info.O_STYLE_CODE = dr["STYLE_CODE"].ToString();
                    yp_info.O_BP_ID = dr["BP_ID"].ToString();
                    yp_info.O_OUT_NAME = dr["OUT_NAME"].ToString();
                    yp_info.O_INNAR_NAME = dr["INNAR_NAME"].ToString();
                    // Image数据暂时不做处理
                    // bpInfo.O_IMAGE = dr["IMAGE"].ToString();
                    yp_info.O_REMARK = dr["REMARK"].ToString();
                    yp_info.O_CY_POS = dr["CY_POS"].ToString();
                    yp_info.O_CY_LEVEL = dr["CY_LEVEL"].ToString();
                    yp_info.O_CJ_PEOPLE = dr["CJ_PEOPLE"].ToString();
                    yp_info.O_CJ_DATE = dr["CJ_DATE"].ToString();
                    yp_info.O_ZL_FILE_ID = dr["ZL_FILE_ID"].ToString();
                }

                return yp_info;
            }
            catch (FormatException e)
            {
                throw e;
            }
            catch (OverflowException e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 通过样品名称查找记录
        /// </summary>
        /// <param name="yp_name"></param>
        /// <returns></returns>
        public YP_INFO_TBL SelectYPInfoByName(string yp_name)
        {
            YP_INFO_TBL yp_info = new YP_INFO_TBL();
            string strSql = "SELECT * FROM dbo.YP_INFO_TBL WHERE YP_NAME ='";
            strSql += yp_name + "'";
            try
            {
                DataTable dt = SQLHelper.ExecuteReaderDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
                foreach (DataRow dr in dt.Rows)
                {
                    yp_info.O_ID = int.Parse(dr["ID"].ToString());
                    yp_info.O_ZL_FILE_ID = dr["ZL_FILE_ID"].ToString();
                    yp_info.O_YP_NAME = yp_name;
                    yp_info.O_YP_CODE = dr["YP_CODE"].ToString();
                    yp_info.O_PM_PATH_NAME = dr["PM_PATH_NAME"].ToString();
                    yp_info.O_PM_PATH_NUM = dr["PM_PATH_NUM"].ToString();
                    yp_info.O_GEO_ATT_NUM = dr["GEO_ATT_NUM"].ToString();
                    if (dr["YP_LAT"].ToString() != "")
                        yp_info.O_YP_LAT = Convert.ToInt32(dr["YP_LAT"].ToString());
                    if (dr["YP_LON"].ToString() != "")
                        yp_info.O_YP_LON = Convert.ToInt32(dr["YP_LON"].ToString());
                    yp_info.O_YP_STYLE = dr["YP_STYLE"].ToString();
                    yp_info.O_STYLE_CODE = dr["STYLE_CODE"].ToString();
                    yp_info.O_BP_ID = dr["BP_ID"].ToString();
                    yp_info.O_OUT_NAME = dr["OUT_NAME"].ToString();
                    yp_info.O_INNAR_NAME = dr["INNAR_NAME"].ToString();
                    // Image数据暂时不做处理
                    // bpInfo.O_IMAGE = dr["IMAGE"].ToString();
                    yp_info.O_REMARK = dr["REMARK"].ToString();
                    yp_info.O_CY_POS = dr["CY_POS"].ToString();
                    yp_info.O_CY_LEVEL = dr["CY_LEVEL"].ToString();
                    yp_info.O_CJ_PEOPLE = dr["CJ_PEOPLE"].ToString();
                    yp_info.O_CJ_DATE = dr["CJ_DATE"].ToString();
                }

                return yp_info;
            }
            catch (FormatException e)
            {
                throw e;
            }
            catch (OverflowException e)
            {
                throw e;
            }
        }

        public DataSet SelectInfoByFileID(string zl_file_id)
        {
            string strSql = "SELECT * FROM dbo.YP_INFO_TBL WHERE ZL_FILE_ID ='";
            strSql += zl_file_id + "'";

            return SQLHelper.ExecuteDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }





        public DataSet SelectByYPID(string ypid)
        {
            string strSql = "SELECT * FROM dbo.YP_INFO_TBL WHERE ID ='";
            strSql += ypid + "'";

            return SQLHelper.ExecuteDataSet(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }










        /// <summary>
        /// 添加样品信息
        /// </summary>
        /// <param name="bb_info"></param>
        /// <returns></returns>
        public int AddYPInfo(YP_INFO_TBL yp_info)
        {
            // 先处理普通数据
            string strSql = "INSERT INTO dbo.YP_INFO_TBL(ZL_FILE_ID,YP_CODE,YP_NAME,PM_PATH_NAME,PM_PATH_NUM,GEO_ATT_NUM,";
            strSql += "YP_LAT,YP_LON,YP_STYLE,STYLE_CODE,BP_ID,OUT_NAME,INNAR_NAME,REMARK,";
            strSql += "CY_POS,CY_LEVEL,CJ_PEOPLE,CJ_DATE) VALUES(";
            strSql += "'" + yp_info.O_ZL_FILE_ID + "',";
            strSql += "'" + yp_info.O_YP_CODE + "',";
            strSql += "'" + yp_info.O_YP_NAME + "',";
            strSql += "'" + yp_info.O_PM_PATH_NAME + "',";
            strSql += "'" + yp_info.O_PM_PATH_NUM + "',";
            strSql += "'" + yp_info.O_GEO_ATT_NUM + "',";
            strSql += yp_info.O_YP_LAT.ToString() + ",";
            strSql += yp_info.O_YP_LON.ToString() + ",";
            strSql += "'" + yp_info.O_YP_STYLE + "',";
            strSql += "'" + yp_info.O_STYLE_CODE + "',";
            strSql += "'" + yp_info.O_BP_ID + "',";
            strSql += "'" + yp_info.O_OUT_NAME + "',";
            strSql += "'" + yp_info.O_INNAR_NAME + "',";
            strSql += "'" + yp_info.O_REMARK + "',";
            strSql += "'" + yp_info.O_CY_POS + "',";
            strSql += "'" + yp_info.O_CY_LEVEL + "',";
            strSql += "'" + yp_info.O_CJ_PEOPLE + "',";
            strSql += "'" + yp_info.O_CJ_DATE.ToString() + "')";

            return SQLHelper.ExecuteNonQuery(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        /// <summary>
        /// 根据文件ID删除数据
        /// </summary>
        /// <param name="file_id"></param>
        /// <returns></returns>
        public int DeleteYPInfo(string file_id)
        {
            string strSql = "DELETE FROM dbo.YP_INFO_TBL WHERE FILE_ID ='";
            strSql += file_id + "'";

            return SQLHelper.ExecuteNonQuery(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        public int DeleteYPInfoById(int id)
        {
            string strSql = "DELETE FROM dbo.YP_INFO_TBL WHERE ID =";
            strSql += id;

            return SQLHelper.ExecuteNonQuery(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        /// <summary>
        /// 根据样品名称删除数据
        /// </summary>
        /// <param name="bb_name"></param>
        /// <returns></returns>
        public int DeleteYPInfoByName(int id)
        {
            string strSql = "DELETE FROM dbo.YP_INFO_TBL WHERE ID =";
            strSql += id.ToString() + "";

            return SQLHelper.ExecuteNonQuery(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="bb_info"></param>
        /// <returns></returns>
        public int UpdateYPInfo(YP_INFO_TBL yp_info)
        {
            string strSql = "UPDATE dbo.YP_INFO_TBL ";
            strSql += "SET ZL_FILE_ID='" + yp_info.O_ZL_FILE_ID + "',";
            strSql += " YP_CODE='" + yp_info.O_YP_CODE + "',";
            strSql += " YP_NAME='" + yp_info.O_YP_NAME + "',";
            strSql += " PM_PATH_NAME='" + yp_info.O_PM_PATH_NAME + "',";
            strSql += " PM_PATH_NUM='" + yp_info.O_PM_PATH_NUM + "',";
            strSql += " GEO_ATT_NUM='" + yp_info.O_GEO_ATT_NUM + "',";
            strSql += " YP_LAT=" + yp_info.O_YP_LAT.ToString() + ",";
            strSql += " YP_LON=" + yp_info.O_YP_LON.ToString() + ",";
            strSql += " YP_STYLE='" + yp_info.O_YP_STYLE + "',";
            strSql += " STYLE_CODE='" + yp_info.O_STYLE_CODE + "',";
            strSql += " BP_ID='" + yp_info.O_BP_ID + "',";
            strSql += " OUT_NAME='" + yp_info.O_OUT_NAME + "',";
            strSql += " INNAR_NAME='" + yp_info.O_INNAR_NAME + "',";
            strSql += " REMARK='" + yp_info.O_REMARK + "',";
            strSql += " CY_POS='" + yp_info.O_CY_POS + "',";
            strSql += " CY_LEVEL='" + yp_info.O_CY_LEVEL + "',";
            strSql += " CJ_PEOPLE='" + yp_info.O_CJ_PEOPLE + "',";
            strSql += " CJ_DATE='" + yp_info.O_CJ_DATE.ToString() + "'";

            strSql += "WHERE ID=" + yp_info.O_ID;

            return SQLHelper.ExecuteNonQuery(SQLHelper.SQLConnString, CommandType.Text, strSql, null);
        }

        #region =影像数据操作=
        public int AppendImage(int id, byte[] bImg, long len)
        {
            string SQL = "UPDATE  dbo.YP_INFO_TBL SET IMAGE = @IMAGE WHERE ID=" + id.ToString();

            SqlParameter[] commandParameters = new SqlParameter[1];
            commandParameters[0] = new SqlParameter("@IMAGE", SqlDbType.Image);
            commandParameters[0].Value = bImg;

            try
            {
                SQLHelper.ExecuteNonQuery(SQLHelper.SQLConnString, CommandType.Text, SQL, commandParameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }


            return 1;
        }

        public byte[] GetImage(int id)
        {
            byte[] photo = null;
            string sql = "SELECT IMAGE FROM dbo.YP_INFO_TBL WHERE ID =" + id.ToString();
            SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.SQLConnString, CommandType.Text, sql, null);//采用SqlDataReader的方法来读取数据

            if (reader.Read())
                photo = reader[0] as byte[];//将第0列的数据写入byte数组 

            reader.Close();                 //关闭reader
            return photo;
        }

        #endregion
    }
}
